package 数组.easy;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/8/7 12:04
 */
public class LuSiSan {

    public static double findMaxAverage(int[] nums, int k) {
        // 用于存储最大连续数组和
        int sum = 0;
        for (int i = 0; i < k; ++i) {
            // 用sum先存储前四个元素和
            sum+=nums[i];
        }
        int temp = sum;
        // 在循环体中继续计算其余连续三个数之和
        for (int i = 1; i+k-1 < nums.length; i++) {
            temp = temp - nums[i-1] + nums[i+k-1];
            if (temp > sum) {
                sum = temp;
            }
        }
        return sum/(double)k;
    }

    public static void main(String[] args) {
        int[] arr = {1,12,-5,-6,50,3};
        double maxAverage = findMaxAverage(arr, 4);
        System.out.println(maxAverage);
    }

}
